package dl.bukkit.myststus.api.playerdata;

import dl.bukkit.myststus.configuration.imp.Config;
import dl.bukkit.myststus.utils.Calculator;
import lk.vexview.event.PlayerClientWindowSizeEvent;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashMap;
import java.util.Map;

public class ClientSizeData {
    private static final Map<Player, ClientSizeData> map = new HashMap<>();

    public static ClientSizeData getInst(Player player) {
        if (! map.containsKey(player)) {
            map.put(player, new ClientSizeData(player));
        }
        return map.get(player);
    }

    public static void leaveServer(PlayerQuitEvent event) {
        map.remove(event.getPlayer());
    }

    private int width;
    private int height;

    private ClientSizeData(Player player) {
    }

    public final void setup(PlayerClientWindowSizeEvent event) {
        this.width = event.getClientWindowWidth();
        this.height = event.getClientWindowHeight();
    }

    public double getValue(String str) {
        str = str
                .replaceAll("\\{" + Config.getInst().getString("Placeholder.ClientWidth") + "}", String.valueOf(width))
                .replaceAll("\\{" + Config.getInst().getString("Placeholder.ClientHeight") + "}", String.valueOf(height));
        return Calculator.isExpression(str) ? Calculator.executeExpression(str) : NumberUtils.toDouble(str);
    }
}
